//
//  NBSelectedController.m
//  聚超值
//
//  Created by 周abel on 16/3/31.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBSelectedController.h"
#import "ABViewCell.h"
#import "ABItem.h"

@interface NBSelectedController() <UIScrollViewDelegate>

@end

@implementation NBSelectedController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.0 注册nib
    [self.tableView registerNib:[UINib nibWithNibName:@"ABViewCell" bundle:nil] forCellReuseIdentifier:@"cell_2"];
    
}
#pragma mark -请求数据
- (void)sendRequest {
    
    // 创建请求路径
    NSString *path = [NSString stringWithFormat: @"http://mrobot.pconline.com.cn/s-300/best/cms/listHomepage.xsp&pageNo=%zd&pageSize=20&v=2.3.0&platform=android", ++self.page];

    // 发送请求
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    // 以JSON格式发送请求
    manger.requestSerializer = [AFJSONRequestSerializer serializer];
    
    // 接受数据解析的模式, 默认为JSON
    manger.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    [manger POST:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        [responseObject writeToFile:@"/Users/zhouabel/Desktop/聚超值/XMGGCZ副本/abel.plist" atomically:YES];
        // 获取NBItem
        NSArray *NBItemarr = responseObject[@"listIndex"];
        // 获取ABItem
        NSArray *ABItemarr = responseObject[@"focus"];

        if (NBItemarr.count ) {
            
            
            for (NSDictionary *dict in NBItemarr) {
                
                NBItem *item = [NBItem mj_objectWithKeyValues:dict[@"value"]];
                
                [self.itemArr addObject:item];
            }
            
            if (ABItemarr.count) {
                
                for (NSDictionary *dict in ABItemarr) {
                    
                    ABItem *item = [ABItem mj_objectWithKeyValues:dict];
                    
                    [self.itemArr_2 addObject:item];
                }

            }
            
            // 刷新UI
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self endRefresh];
                [self.tableView reloadData];
            });
        } else {
        
            // 数据为空时处理
            [self.tableView.mj_footer endRefreshingWithNoMoreData];

        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
    }];
     
     }

#pragma mark -tableView数据源方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 1.0获取cell
    if (indexPath.row == 0) {
        
        // 设置cell高度
        tableView.rowHeight = 160;
        
        ABViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:@"cell_2"];
        
        // 2.0 设置数据
        cell.itemArr = self.itemArr_2;
        
        // 3.0 返回cell
        return cell;

    } else {
        
        NBCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_1"];

        tableView.rowHeight = 120;
        
        if (self.itemArr.count) {
            // 获取对应的item
            NBItem *item = self.itemArr[indexPath.row];
            // 2.0 设置数据
            cell.item = item;
        }
        
        // 3.0 返回cell
        return cell;

    }
    
    
}


#pragma mark -监听scrollView的滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 发布通知,通知scrollView停止滚动
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stop" object:nil];
    
    // 代理方法
    if ([self.delegate respondsToSelector:@selector(selected:Distance:)]) {
        [self.delegate selected:self Distance:scrollView.contentOffset.y];
    }
}
// 减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    // 发布通知,通知scrollView滚动
    [[NSNotificationCenter defaultCenter] postNotificationName:@"scroll" object:nil];
    
}
// 停止拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    // 发布通知,通知scrollView滚动
    [[NSNotificationCenter defaultCenter] postNotificationName:@"scroll" object:nil];
}
@end
